package com.xbongbong.paas.redis;

import io.lettuce.core.metrics.CommandLatencyCollectorOptions;
import io.lettuce.core.resource.DefaultClientResources;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.scripting.support.ResourceScriptSource;
import org.springframework.util.ClassUtils;

/**
 * redis 配置
 * @author kaka
 * @time 2018-9-5 21:20
 */
@Configuration
public class RedisCacheAutoConfiguration {

    /**
     * 修改lecttuce的配置，关闭CommandLatency的监控（目前我们没有强述求），目前发现有内存溢出问题
     * lettuce-core版本5.3.7.RELEASE
     * @return 覆盖spring-boot-autoconfigure预备注入的ClientResources Bean
     */
    @Bean(destroyMethod = "shutdown")
    DefaultClientResources lettuceClientResources() {
        if (ClassUtils.hasMethod(CommandLatencyCollectorOptions.class, "disabled")) {
            return DefaultClientResources.builder()
                    .commandLatencyCollectorOptions(CommandLatencyCollectorOptions.disabled())
                    .build();
        } else {
            return DefaultClientResources.builder().build();
        }

    }

    @Bean
    public DefaultRedisScript<String> redisImportStatusUpdateScript() {
        DefaultRedisScript<String> redisScript = new DefaultRedisScript<>();
        redisScript.setScriptSource(new ResourceScriptSource(new ClassPathResource("scripts/import_status_update.lua")));
        redisScript.setResultType(String.class);
        return redisScript;
    }

    @Bean
    public DefaultRedisScript<Long> redisImportFragmentationNumDecrScript() {
        DefaultRedisScript<Long> redisScript = new DefaultRedisScript<>();
        redisScript.setScriptSource(new ResourceScriptSource(new ClassPathResource("scripts/import_fragmentation_num_decr.lua")));
        redisScript.setResultType(Long.class);
        return redisScript;
    }

    /**
     * 业务规则解锁
     * @return
     */
    @Bean
    public DefaultRedisScript<Long> businessRuleUnLock() {
        DefaultRedisScript<Long> redisScript = new DefaultRedisScript<>();
        redisScript.setScriptSource(new ResourceScriptSource(new ClassPathResource("scripts/unlock.lua")));
        redisScript.setResultType(Long.class);
        return redisScript;
    }

    /**
     * 导入判重脚本
     */
    @Bean
    public DefaultRedisScript<Boolean> redisImportRepeatScript() {
        DefaultRedisScript<Boolean> redisScript = new DefaultRedisScript<>();
        redisScript.setScriptSource(new ResourceScriptSource(new ClassPathResource("scripts/import_repeat.lua")));
        redisScript.setResultType(Boolean.class);
        return redisScript;
    }

    /**
     * 产品成本加锁
     */
    @Bean
    public DefaultRedisScript<Boolean> redisProductCostsLock() {
        DefaultRedisScript<Boolean> redisScript = new DefaultRedisScript<>();
        redisScript.setScriptSource(new ResourceScriptSource(new ClassPathResource("scripts/product_costs_lock.lua")));
        redisScript.setResultType(Boolean.class);
        return redisScript;
    }
    /**
     * 产品成本解锁
     */
    @Bean
    public DefaultRedisScript<Boolean> redisProductCostsUnLock() {
        DefaultRedisScript<Boolean> redisScript = new DefaultRedisScript<>();
        redisScript.setScriptSource(new ResourceScriptSource(new ClassPathResource("scripts/product_costs_unlock.lua")));
        redisScript.setResultType(Boolean.class);
        return redisScript;
    }

    /**
     * 产品库存校验
     **/
    @Bean
    public DefaultRedisScript<Boolean> redisProductStockLock() {
        DefaultRedisScript<Boolean> redisScript = new DefaultRedisScript<>();
        redisScript.setScriptSource(new ResourceScriptSource(new ClassPathResource("scripts/product_stock_lock.lua")));
        redisScript.setResultType(Boolean.class);
        return redisScript;
    }

    /**
     * 产品库存校验
     **/
    @Bean
    public DefaultRedisScript<Boolean> redisSingleProductStockLock() {
        DefaultRedisScript<Boolean> redisScript = new DefaultRedisScript<>();
        redisScript.setScriptSource(new ResourceScriptSource(new ClassPathResource("scripts/product_stock_single_lock.lua")));
        redisScript.setResultType(Boolean.class);
        return redisScript;
    }
}